package com.lujianfei.common

import androidx.databinding.ObservableArrayList

object DataBindingDifferUtils {

    inline fun <reified T:Any> updateList(oldList: ObservableArrayList<T>, newList:List<T>?) {
        if (newList == null) return
        val newSize = newList.size
        val oldSize = oldList.size

        val minSize = Math.min(newSize,oldSize)
        val maxSize = Math.max(newSize,oldSize)
        for (idx in 0 until minSize) {
            oldList[idx] = newList[idx]
        }
        val offset = newSize - oldSize
        if (offset > 0) {
            for (idx in minSize until maxSize) {
                oldList.add(newList[idx])
            }
        } else {
            for (idx in (maxSize-1) downTo minSize) {
                oldList.removeAt(idx)
            }
        }
    }
}
